package uto.phone.flash.wind.app

import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import uto.phone.flash.wind.R
import uto.phone.flash.wind.base.IActivity
import uto.phone.flash.wind.base.IAdapter
import uto.phone.flash.wind.base.IHolder
import uto.phone.flash.wind.base.findView

class HomeTopHolder(group: ViewGroup,activity: IActivity? = null,adapter: IAdapter? = null): IHolder(group,activity,adapter){
    private lateinit var rvContent: RecyclerView
    private lateinit var itemAdapter: IAdapter
    private lateinit var dataList: MutableList<UserBean>

    override fun layoutRes(): Int {
        return R.layout.layout_home_top
    }

    override fun initLayout() {
        rvContent = findView(R.id.rvContent)
        dataList = getData()
        itemAdapter = object: IAdapter(dataList,activity){
            override fun createHolder(parent: ViewGroup, viewType: Int): IHolder {
                return object: IHolder(parent,activity,this){
                    private lateinit var ivImage: ImageView
                    private lateinit var tvName: TextView
                    private lateinit var bean: UserBean

                    override fun layoutRes(): Int {
                        return R.layout.layout_home_top_item
                    }

                    override fun initLayout() {
                        ivImage = itemView.findViewById(R.id.ivImage)
                        tvName = itemView.findViewById(R.id.tvName)
                        bean = UserBean()
                    }

                    override fun bindData(any: Any?) {
                        bean = any as? UserBean ?: bean
                        ivImage.setImageResource(bean.head)
                        tvName.text = bean.name
                    }

                    override fun onClick(v: View?) {
                        // 跳转下页
                        val intent = Intent(activity ?: return,NextActivity::class.java)
                        intent.putExtra("title",bean.name)
                        activity?.startActivity(intent)
                    }
                }
            }
        }
        rvContent.adapter = itemAdapter
    }

    private fun getData(): MutableList<UserBean>{
        val list = mutableListOf<UserBean>()
        list.add(UserBean("Beauty",head = R.mipmap.ic_1))
        list.add(UserBean("Pets",head = R.mipmap.ic_2))
        list.add(UserBean("Anim",head = R.mipmap.ic_3))
        list.add(UserBean("Lovers",head = R.mipmap.ic_4))
        list.add(UserBean("Funny",head = R.mipmap.ic_5))
        list.add(UserBean("Car",head = R.mipmap.ic_6))
        list.add(UserBean("Superhero",head = R.mipmap.ic_7))
        list.add(UserBean("Sports",head = R.mipmap.ic_8))
        return list
    }

}